﻿using System;
using System.Text.RegularExpressions;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;
using Yunzuan.Foundation.Log;
using YunZuan.Models.Entities;

namespace Yunzuan.Foundation.Web
{
    public static class SMSManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tel"></param>
        /// <param name="lib">SMSLib</param>
        /// <param name="param">各自短信内容替换的参数</param>
        /// <returns></returns>
        public static string SendSms(string tel, SMSLib lib, HashParam param)
        {
            var smsContent = "";
            var tempSmsCode = "";
            var smsSign = "钻石系统";    // 系统自定义签名
            if (string.IsNullOrEmpty(tel) || !Regex.IsMatch(tel, "^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(14[0-9]{1})|(18[0-9]{1}))+\\d{8})$"))
            {
                return "";
            }
            switch (lib)
            {
                case SMSLib.SMSTemplateByReg:
                    smsSign = "注册验证";
                    tempSmsCode = GlobalConfig.SMSTemplateByReg;
                    var code = "";
                    if (System.Web.HttpContext.Current.Cache.Get("Cache:smsCode" + tel) != null)
                        code = System.Web.HttpContext.Current.Cache.Get("Cache:smsCode" + tel).ToString();
                    else
                        code=(new Random()).Next(1000, 9999).ToString();
                    //SessionManager.SetSession("smsCode", code);
                    //绝对时间  超过1分钟就超时
                    System.Web.HttpContext.Current.Cache.Insert("Cache:smsCode" + tel, code, null,DateTime.Now.AddMinutes(10),System.Web.Caching.Cache.NoSlidingExpiration);
                    smsContent = GlobalConfig.SMSTemplateByRegContent.Replace("#", "\"").Replace("${code}", code).Replace("${min}", "10");  //10min
                    break;
                case SMSLib.SMSLogin:
                    //smsSign = "登陆验证";
                    tempSmsCode = GlobalConfig.SMSLogin;
                    var loginCode = "";
                    if (System.Web.HttpContext.Current.Cache.Get("Cache:loginCode" + tel) != null)
                        loginCode = System.Web.HttpContext.Current.Cache.Get("Cache:loginCode" + tel).ToString();
                    else
                        loginCode = (new Random()).Next(1000, 9999).ToString();
                    //SessionManager.SetSession("smsCode", code);
                    //绝对时间  超过1分钟就超时
                    System.Web.HttpContext.Current.Cache.Insert("Cache:loginCode" + tel, loginCode, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration);
                    smsContent = GlobalConfig.SMSLoginContent.Replace("#", "\"").Replace("${code}", loginCode);  //10min
                    break;
                //case SMSLib.SMSTemplateByGet:
                //    tempSmsCode = GlobalConfig.SMSTemplateByGet;
                //    smsContent = GlobalConfig.SMSTemplateByGetContent.Replace("#", "\"").Replace("${customer}", param.GetString("customer"));
                //    break;
                //case SMSLib.SMSTemplateByArrived:
                //    tempSmsCode = GlobalConfig.SMSTemplateByArrived;
                //    smsContent = GlobalConfig.SMSTemplateByArrivedContent.Replace("#", "\"").Replace("${salesname}", param.GetString("salesname"));
                //    break;
                case SMSLib.SMSTemplateByOrders:
                    tempSmsCode = GlobalConfig.SMSTemplateByOrders;
                    smsContent = GlobalConfig.SMSTemplateByOrdersContent.Replace("#", "\"").Replace("${salesname}", param.GetString("salesname")).Replace("${customer}", param.GetString("customer")).Replace("${time}", param.GetString("time")).Replace("${sn}", param.GetString("sn"));
                    break;

                case SMSLib.SMSTemplateByHasReg:
                    //smsSign = "注册验证";
                    tempSmsCode = GlobalConfig.SMSTemplateByHasReg;
                    smsContent = GlobalConfig.SMSTemplateByHasRegContent.Replace("#", "\"").Replace("${adminname}", param.GetString("adminname")).Replace("${name}", param.GetString("name"));
                    break;

                case SMSLib.SMSArrivalOrders:
                    //smsSign = "到货通知（新）";
                    tempSmsCode = GlobalConfig.SMSArrivalOrders;
                    smsContent = GlobalConfig.SMSArrivalOrdersContent.Replace("#", "\"").Replace("${salesname}", param.GetString("salesname")).Replace("${supplier}", param.GetString("supplier"));
                    break;
                //case SMSLib.SMSTemplateByArrived:
                //    tempSmsCode = GlobalConfig.SMSTemplateByArrived;
                //    smsContent = GlobalConfig.SMSTemplateByArrivedContent.Replace("#", "\"").Replace("${salesname}", param.GetString("salesname"));
                //    break;
                case SMSLib.SMSCustomerOpenAPI:
                    tempSmsCode = GlobalConfig.SMSCustomerOpenAPI;  //customername  统一为customer
                    smsContent = GlobalConfig.SMSCustomerOpenAPIContent.Replace("#", "\"").Replace("${name}", param.GetString("name")).Replace("${customer}", param.GetString("customer"));
                    break;

                case SMSLib.SMSTemplateByReview:
                    tempSmsCode = GlobalConfig.SMSTemplateByReview;  //customername  统一为customer
                    smsContent = GlobalConfig.SMSTemplateByReviewContent.Replace("#", "\"").Replace("${customername}", param.GetString("customer"));
                    break;
                case SMSLib.SMSTemplateByOrderArrival:
                    tempSmsCode = GlobalConfig.SMSTemplateByOrderArrival;  //customername  统一为customer
                    smsContent = GlobalConfig.SMSTemplateByOrderArrivalContent.Replace("#", "\"").Replace("${salesname}", param.GetString("salesname"));
                    break;
                case SMSLib.SMSLendOrders:
                    tempSmsCode = GlobalConfig.SMSTemplateByLendOrder;  //customername  统一为customer
                    smsContent = GlobalConfig.SMSTemplateByLendOrderContent.Replace("#", "\"").Replace("${salesname}", param.GetString("salesname")).Replace("${cutomername}", param.GetString("cutomername"));
                    break;

                case SMSLib.SMSSubmitOrderError:
                    tempSmsCode = GlobalConfig.SMSSubmitOrderError;  //customername  统一为customer
                    smsContent = GlobalConfig.SMSSubmitOrderErrorContent.Replace("#", "\"").Replace("${name}", param.GetString("name")).Replace("${supplier}", param.GetString("supplier")).Replace("${sn}", param.GetString("sn"));
                    break; 
            }
            string url = GlobalConfig.SMSurl;
            string appkey = GlobalConfig.SMSappkey;
            string secret = GlobalConfig.SMSsecret;

            ITopClient client = new DefaultTopClient(url, appkey, secret);
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
            req.Extend = "123456";
            req.SmsType = "normal";
            req.SmsFreeSignName = smsSign;
            req.SmsParam = smsContent;
            req.RecNum = tel;
            req.SmsTemplateCode = tempSmsCode;
            AlibabaAliqinFcSmsNumSendResponse rsp = client.Execute(req);
            if (!rsp.Body.Contains("<success>true</success>"))  //发送不成功
                Logger.Warn(typeof(SMSManager), "method:SendSms," + rsp.Body);
            return rsp.Body;
        }
    }
    public enum SMSLib
    {
        /// <summary>
        /// 注册验证码模板   参数HashParam为： 验证码 code
        /// </summary>
        SMSTemplateByReg,
        ///// <summary>
        ///// 取货提醒模板  参数HashParam为： 客户 customer
        ///// </summary>
        //SMSTemplateByGet,
        ///// <summary>
        ///// 到货提醒模板  参数HashParam为：  业务 salesname
        ///// </summary>
        //SMSTemplateByArrived,
        /// <summary>
        /// 下单提醒模板  参数HashParam为： 客户 customer,业务 salesname,货号 sn ,下单时间time
        /// </summary>
        SMSTemplateByOrders,
        /// <summary>
        /// 已注册提醒模板  参数HashParam为： 管理员adminname 用户 name
        /// </summary>
        SMSTemplateByHasReg,
        /// <summary>
        /// 采购成功提醒模版   参数HashParam为： 客户 customer
        /// </summary>
        //SMSTemplateByPurchase,
        /// <summary>
        /// 审批通过成功提醒模板 客户 customername
        /// </summary>
        SMSTemplateByReview,
        /// <summary>
        /// 到货提醒模版
        /// </summary>
        SMSTemplateByOrderArrival,
        /// <summary>
        /// 开通API接口短信
        /// </summary>
        SMSCustomerOpenAPI,
        /// <summary>
        /// 到货提醒模板-新 (带供应商)
        /// </summary>
        SMSArrivalOrders,

        /// <summary>
        /// 多级下单失败提醒业务
        /// </summary>
        SMSSubmitOrderError,

         /// <summary>
        /// 借货单
        /// </summary>
        SMSLendOrders,
        /// <summary>
        /// 登陆
        /// </summary>
        SMSLogin

    }

}
